package com.ccb.sc.member.controller;


import com.ccb.sc.auth.TokenAuth;
import com.ccb.sc.auth.TokenModel;
import com.ccb.sc.member.service.ReceiverService;
import com.ccb.sc.member.vo.ReceiverCreateVo;
import com.ccb.sc.member.vo.ReceiverVo;
import com.ccb.sc.common.bind.annotation.CurrentUser;
//psl import com.ccb.sc.common.bind.exception.BizException;
import com.ccb.sc.common.bind.response.ResBody;
import com.ccb.sc.common.code.BizCodeFace;
import com.ccb.sc.common.code.ErrorCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * <p>
 * 收货地址 前端控制器
 * </p>
 *
 * @author ken
 * @since 2020-09-25
 */
@RestController
@TokenAuth
@RequestMapping("/app/receiver")
@Api(value = "收货地址管理", tags = "收货地址管理")
public class ReceiverController {

    private ReceiverService receiverService;

    @PostMapping("/add")
    @ApiOperation("新增")
    public ResBody<?> createNew(@CurrentUser @ApiIgnore TokenModel token,
                             @RequestBody @Validated ReceiverCreateVo createVo) {
        Long memberId = token.getId();
        boolean result = this.receiverService.createNew(memberId, createVo);
        if (!result) {
            //psl throw new BizException(BizCodeFace.createBizCode(ErrorCode.FAIL));
        }
        return ResBody.success();
    }

    @PutMapping("/update")
    @ApiOperation("修改")
    public ResBody<?> update(@CurrentUser @ApiIgnore TokenModel token,
                          @RequestBody @Validated ReceiverVo updateVo) {
        Long memberId = token.getId();
        boolean result = this.receiverService.update(memberId, updateVo);
        if (!result) {
            //psl throw new BizException(BizCodeFace.createBizCode(ErrorCode.FAIL));
        }
        return ResBody.success();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除")
    public ResBody<?> cancelFavor(@CurrentUser @ApiIgnore TokenModel token, @PathVariable Long id) {
        boolean result = this.receiverService.delete(token.getId(), id);
        if (!result) {
            //psl throw new BizException(BizCodeFace.createBizCode(ErrorCode.FAIL));
        }
        return ResBody.success();
    }

    @PostMapping("/default")
    @ApiOperation("设置默认地址")
    public ResBody<?> setDefault(@CurrentUser @ApiIgnore TokenModel tokenModel, @RequestParam Long id) {
        boolean result = this.receiverService.setDefault(tokenModel.getId(), id);
        if (!result) {
            //psl throw new BizException(BizCodeFace.createBizCode(ErrorCode.FAIL));
        }
        return ResBody.success();
    }

    @GetMapping("/list")
    @ApiOperation(value = "列表", responseContainer = "list", response = ReceiverVo.class)
    public ResBody<?> favorList(@CurrentUser @ApiIgnore TokenModel token) {
        List<ReceiverVo> list = this.receiverService.findAll(token.getId());
        return ResBody.success(list);
    }

    @Autowired
    public void setReceiverService(ReceiverService receiverService) {
        this.receiverService = receiverService;
    }
}

